﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class RoleManagerController : Controller
    {
        //
        // GET: /RoleManager/

        public ActionResult RoleIndex()
        {
            return View();
        }

        public ActionResult AddRole()
        {
            return View();
        }
        private RolesInfo roleinfo = new RolesInfo(true);

        public ActionResult GetRoleList(PageVM pvm)
        {
            IList<RolesInfo> rolelist = roleinfo.GetAllRoles(pvm.rp, pvm.page);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = roleinfo._RolesCount;
            List<string> v;
            foreach (RolesInfo item in rolelist)
            {
                #region
                v = new List<string>();
                v.Add(item.RoleId);
                v.Add(item.RoleName);
                v.Add(item.RoleMeno);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.RoleId,
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }
        /// <summary>
        /// 保存角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveRoles(RolesVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                RolesInfo srole = new RolesInfo()
                {
                    RoleId = model.RoleId,
                    RoleName = model.RoleName,
                    RoleMeno = string.IsNullOrEmpty(model.RoleMeno) ? "" : model.RoleMeno
                };
                result = roleinfo.SaveRole(srole);
            }
            return Json(result);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteRoles(string roleid)
        {
            int result = 0;
            result = roleinfo.DeleteRole(roleid);
            return Json(result);
        }

        //=====================================================================
        //  角色绑定到菜单
        //=====================================================================
        public ActionResult RoleBindModular()
        {
            return View();
        }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetMenuList()
        {
            ModularInfo muinfo = new ModularInfo();
            IList<ModularInfo> mlist = muinfo.GetModularAll();
            List<JsonTreeNode> nodes = new List<JsonTreeNode>();
            foreach (var item in mlist)
            {
                JsonTreeNode cnode = new JsonTreeNode();
                cnode.id = item.ModularId;
                cnode.text = item.ModularName;
                cnode.value = item.ModularUrl == null ? "" : item.ModularUrl;
                cnode.hasChildren = false;
                cnode.classes = "system";
                cnode.isexpand = false;
                cnode.complete = true;
                nodes.Add(cnode);
            }
            return Json(nodes);
        }
        /// <summary>
        /// 获取所有模块
        /// </summary>
        /// <returns></returns>
        public IList<ModularInfo> GetModularForCheckBox()
        {
            ModularInfo muinfo = new ModularInfo();
            IList<ModularInfo> mlist = muinfo.GetModularAll();
            return mlist;
        }
        /// <summary>
        /// 根据角色ID获取模块
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public IList<ModularInfo> GetModularForRole(string roleid)
        {           
            ModularInfo muinfo = new ModularInfo();
            IList<ModularInfo> mlist = muinfo.GetModularByRole(roleid);
            return mlist;            
        }

        /// <summary>
        /// 根据角色ID获取
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public IList<MenuInfo> GetMenuInfoForRole(string roleid)
        {
            MenuInfo meinfo = new MenuInfo();
            IList<MenuInfo> mlist = meinfo.GetBindedMenu(roleid);
            return mlist;
        }

        /// <summary>
        /// 根据所有子菜单
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public IList<MenuInfo> GetMenuForModular()
        {
            string query = "where PNodeID <> 0";
            MenuInfo minfo = new MenuInfo();
            IList<MenuInfo> melist = minfo.GetMenuBy(query);
            return melist;
        }
        /// <summary>
        /// 先清理绑定的角色菜单
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult ClearBindMenuToRole(string roleid)
        {
            MenuInfo mdinfo = new MenuInfo();
            int rowcount = mdinfo.ClearBindMenuToRole(roleid);
            return Json(rowcount);
        }
        /// <summary>
        /// 绑定模块到角色
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="logid"></param>
        /// <returns></returns>
        public ActionResult BindMenuToRole(int mid, string roleid)
        {
            MenuInfo mdinfo = new MenuInfo();            
            int rows = mdinfo.BindMenuToRole(roleid,mid);
            return Json(rows);
        }
        /// <summary>
        /// 删除绑定
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult NotBindModularToRole(string mid, string roleid)
        {
            ModularInfo mdinfo = new ModularInfo();
            int rows = mdinfo.NotBindModular(mid, roleid);
            return Json(rows);
        }

        public ActionResult AddRoleToModular(string roleid)
        {
            ViewData["allmodular"] = GetModularForCheckBox();
            ViewData["modularforlog"] = GetModularForRole(roleid);
            ViewData["selrole"] = roleid;
            ViewData["allmenu"] = GetMenuForModular();       
            ViewData["selmenu"] = GetMenuInfoForRole(roleid);
            return View();
        }
        //=====================================================================
        //  角色绑定到菜单
        //=====================================================================
        /*
        public ActionResult RoleBindMenu()
        {
            return View();
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllMenuList()
        {
            MenuInfo meinfo = new MenuInfo();
            IList<MenuInfo> mlist = meinfo.GetMenuBy("where PNodeID = 0");
            List<JsonTreeNode> nodes = new List<JsonTreeNode>();
            foreach (var item in mlist)
            {
                JsonTreeNode cnode = new JsonTreeNode();
                cnode.id = item.Sid.ToString();
                cnode.text = item.Title;
                cnode.value = item.Url == null ? "" : item.Url;
                cnode.hasChildren = false;
                cnode.classes = "system";
                cnode.isexpand = false;
                cnode.complete = true;
                AddChildNode(cnode);
                nodes.Add(cnode);
            }
            return Json(nodes);
        }

        /// <summary>
        /// 递归添加子节点
        /// </summary>
        /// <param name="node"></param>
        private void AddChildNode(JsonTreeNode node)
        {
            int pid = Convert.ToInt32(node.id);
            MenuInfo menu = new MenuInfo();
            IList<MenuInfo> mlist =
                menu.GetMenuBy("where PNodeID <> 0").Where(minfo => minfo.PnodeId == pid).ToList();
            if (mlist != null)
            {
                foreach (var Childitem in mlist)
                {
                    node.hasChildren = true;
                    JsonTreeNode nodeChild = new JsonTreeNode();
                    nodeChild.id = Childitem.Sid.ToString();
                    nodeChild.text = Childitem.Title;
                    nodeChild.value = Childitem.Url == null ? "" : Childitem.Url;
                    nodeChild.hasChildren = false;
                    nodeChild.isexpand = true;
                    node.ChildNodes.Add(nodeChild);
                }
            }
        }

        /// <summary>
        /// 删除绑定
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="meid"></param>
        /// <returns></returns>
        public ActionResult NotBindMenuToRole(string roleid, int meid)
        {
            MenuInfo meu = new MenuInfo();
            int rowcount = meu.NotBindMenuToRole(roleid, meid);
            return Json(rowcount);
        }
        */
    }
}
